import { _decorator, Component, Node, Label, Sprite, SpriteFrame, Widget } from "cc";
import { ResUtils } from "../ResUtils";

const { ccclass, property } = _decorator;

interface IAdData {
    logoUrl: string
    imgUrlList: Array<string>
    adId: string | number
    icon: string
    desc: string
    title: string
}

@ccclass
export class NativeAdView extends Component {
    @property(Node)
    adImg: Node = null;

    @property(Node)
    adLogo: Node = null;

    @property(Node)
    adTitle: Node = null;

    @property(Node)
    adDesc: Node = null;

    @property(Node)
    adIcon: Node = null;

    @property(Node)
    adClose: Node = null;
    adData: IAdData = null;

    adClickCallBack: Function = null;
    adCloseCallBack: Function = null;

    constructor() {
        super();
    }

    initView(adData) {
        this.adData = adData;
        this.showAd();
    }

    showAd(): void {
        if (!this.adData) return;
        //console.log('this.adData:' + JSON.stringify(this.adData));
        // 加载广告图片
        // this.adImg.active = false;
        this.adData.imgUrlList &&
            this.adData.imgUrlList.length > 0 &&
            ResUtils.loadImgUrl(this.adData.imgUrlList[0].split("?")[0], (texture) => {
                if (texture)
                    this.showAdImg(texture);
            });

        // 加载广告 logo
        this.adLogo.active = false;
        this.adData.logoUrl &&
            ResUtils.loadImgUrl(this.adData.logoUrl, (texture) => {
                this.showAdLogo(texture);
            });
        //显示广告名称
        if (!!this.adTitle) {
            this.adData.title && this.showAdTitle(this.adData.title);
        }
        //显示广告描述
        if (!!this.adDesc) {
            this.adData.desc && this.showAdDesc(this.adData.desc);
        }

    }

    showAdTitle(title) {
        //console.log("showAdTitle");
        this.adTitle.active = true;
        this.adTitle.getComponent(Label).string = title;
    }

    showAdDesc(desc) {
        //console.log("showAdDesc");
        this.adDesc.active = true;
        this.adDesc.getComponent(Label).string = desc;
    }

    showAdImg(texture) {
        //console.log("showAdImg");
        if (!!this.adClose) {
            this.adClose.active = true;
        }
        this.adImg.getComponent(Sprite).spriteFrame = SpriteFrame.createWithImage(texture);
    }

    showAdLogo(texture) {
        //console.log("showAdLogo");
        this.adLogo.active = true;
        this.adLogo.getComponent(Sprite).spriteFrame = SpriteFrame.createWithImage(texture);
    }

    showAdIcon(texture) {
        //console.log("showAdIcon");
        this.adIcon.active = true;
        this.adIcon.getComponent(Sprite).spriteFrame = SpriteFrame.createWithImage(texture);
    }

    onNativeAdClick(): void {
        console.log("onNativeAdClick");
        // this.node.active = false;
        if (!!this.adClickCallBack) {
            this.adClickCallBack();
        }
    }

    onEventAdClose() {
        this.node.active = false;
        if (!!this.adCloseCallBack) {
            this.adCloseCallBack();
        }
    }

}
